<#assign tit><#if _oper='add'>新增<#else>修改</#if>私教礼包</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  var ss_del = '<img src="${_appPath!}/images/del.png" onclick="deli(this);" style="cursor:pointer;"/>';

  function deli(e) {
    var tr = e.parentNode.parentNode;
    var aid = tr.getAttribute("aid");
    if(aid)
      tr.style.display = "none";
    else
      tr.parentNode.deleteRow(tr.rowIndex);
  }

  function addit() {
    var tb = getEl("tb_item");
    if(!tb) return;
    var tr = tb.insertRow(-1);
    tr.className = (tb.rows.length%2?"line1":"line2");
    var td = tr.insertCell(-1);
    td.innerHTML = '<input type="text" value="" size="7" maxlength="20" onchange="chkProd(this);"><input type="button" class="opwin" value="..." onclick="selProd(this);"/>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="text" value="" size="30" readonly style="border:1px solid #ddd;">';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="text" value="" size="4" maxlength="4">';
    td = tr.insertCell(-1);
    td.innerHTML = '<input type="text" value="" size="6" maxlength="8">';
    td = tr.insertCell(-1);
    td.innerHTML = ss_del;
  }

  var _slc;
  function selProd(ipt) {
    _slc = ipt.previousSibling;
    openWin("getProduct", 650, 400);
  }
  function addProduct(row) {
    _slc.value = row.cells[0].innerHTML;
    var tr = _slc.parentNode.parentNode;
    tr.cells[1].firstChild.value = row.cells[1].innerHTML;
    tr.cells[2].firstChild.value = "1";
    tr.cells[3].firstChild.value = row.cells[3].innerHTML;
  }
  function chkProd(ipt) {
    if(ipt.value!="") {
      getJson("getProductInfo?code="+ipt.value, function(data){
        if(data.ret) {
          ipt.value = data.data.code;
          var tr = ipt.parentNode.parentNode;
          tr.cells[1].firstChild.value = data.data.name;
          tr.cells[2].firstChild.value = "1";
          tr.cells[3].firstChild.value = data.data.price;
        } else {
          window.alert(data.msg);
        }
      });
    }
  }


  function save(fm) {
    if(!dotbd(fm.items, "tb_item", ["商品编号","","商品数量","商品总价"])) return false;
    return post(fm);
  }
  function dotbd(ipt, tbid, cl) {
    var tb = getEl(tbid);
    if(!tb) return false;
    var i, j, aid, el, ss, tr, arr = [];
    for(j=1; j<tb.rows.length; j++) {
      tr = tb.rows[j];
      aid = tr.getAttribute("aid");
      if(tr.style.display=="none") {
        if(aid) arr.push(aid+",-");
      } else {
        for(i=0; i<cl.length; i++) {
          if(!cl[i]) continue;
          el = tr.cells[i].firstChild;
          if(el.tagName=="INPUT" && (el.type=="radio" || el.type=="checkbox")) {
            continue;
          }
          if(el.value=="" && cl[i]!="") {
            window.alert("请"+(el.tagName=="SELECT"?"选择":"输入")+cl[i]);
            try {el.focus();} catch(e) {}
            return false;
          }
        }
        ss = aid?aid:"";
        for(i=0; i<cl.length; i++) {
          if(!cl[i]) continue;
          ss += ",";
          el = tr.cells[i].firstChild;
          if(!(el.tagName=="INPUT" && (el.type=="radio" || el.type=="checkbox") && !el.checked))
            ss += el.value;
        }
        arr.push(ss);
      }
    }
    ipt.value = arr.join("|");
    return true;
  }

  function selAllShop(v) {
    var cbs = document.getElementsByName("shops");
    if(cbs && cbs.length>0) {
      for(var i=0; i<cbs.length; i++)
        cbs[i].checked = v;
    }
  }
</script>
<div style="width:900px;">

  <form id="fm_input" method="post" action="<#if _oper='add'>_add<#else>_edit</#if>" onsubmit="return save(this);">
    <input type="hidden" name="items" value="">
    <div class="kbox">
      <div style="width:540px; float:left;">
        <table class="input w4">
          <tr>
            <th width="85">编号</th>
            <td width="160"><#if _oper='add'><input type="text" name="code" value="${pkg.code!}" req="编号" size="12" maxlength="20"><#else><input type="hidden" name="code" value="${pkg.code!}">${pkg.code!}</#if></td>
            <th width="85"></th>
            <td width="160"></td>
          </tr>
          <tr>
            <th>名称</th>
            <td colspan="3"><input type="text" name="chName" value="${pkg.chName!}" req="名称" class="w2" " maxlength="50"></td>
          </tr>
          <tr>
            <th>启用日期</th>
            <td><input type="text" class="date" name="activeTime" value="${pkg.activeTime!}" onclick="cal(this);" size="10" maxlength="10"></td>
            <th>结束时间</th>
            <td><input type="text" class="date" name="inactiveTime" value="${pkg.inactiveTime!}" onclick="cal(this);" size="10" maxlength="10"></td>
          </tr>
          <tr>
            <th>销售价格</th>
            <td><input type="text" name="totalPrice" value="${pkg.totalPrice!}" req="销售价格" size="10" maxlength="8"></td>
            <th>状态</th>
            <td><label><input type="radio" name="status" value="1"<#if pkg.status=1> checked</#if>>正常</label>
              <label><input type="radio" name="status" value="2"<#if pkg.status!=1> checked</#if>>停用</label></td>
          </tr>
          <tr>
            <th>备注</th>
            <td colspan="3"><textarea name="note" cols="40" rows="3">${pkg.note!}</textarea></td>
          </tr>
          <tr><td colspan="4"><hr size="1" style="border-top:1px solid #ccc;"></td></tr>
          <tr>
            <th>私教课程</th>
            <td colspan="3"><input type="hidden" name="autoId" value="${pt.autoId!}"><select name="itemCode" class="w2" req="私教课程"><option value="">--请选择--</option><#if ptCodeList??><#list ptCodeList as t><option value="${t.code!}"<#if pt.itemCode?? && pt.itemCode=t.code> selected</#if>>${t.code!} - ${t.chName!}</option></#list></#if></select></td>
          </tr>
          <tr>
            <th>数量</th>
            <td><input type="text" name="quantity" value="${pt.quantity!}" maxlength="6" req="数量"></td>
            <th>价格</th>
            <td><input type="text" name="price" value="${pt.price!}" maxlength="8" req="价格"></td>
          </tr>
          <tr>
            <th>有效天数</th>
            <td><input type="text" name="validDays" value="${pt.validDays!}" maxlength="6" req="有效天数"></td>
            <th>教练等级</th>
            <td><select name="coachLevel"><option value="">不限</option><#if coachLevel??><#list coachLevel as t><option value="${t.id}"<#if pt.coachLevel?? && pt.coachLevel=t.id> selected</#if>>${t.name}</option></#list></#if></select></td>
          </tr>
        </table>

        <div style="margin:0 20px;">
          <div class="lsinfo">包含商品</div>
          <table id="tb_item" class="list">
            <tr>
              <th width="23%">编号</th>
              <th width="45%" class="left">名称</th>
              <th width="12%">数量</th>
              <th width="15%">总价</th>
              <th></th>
            </tr>
            <#if itemList??><#list itemList as c>
            <tr<#if c_index%2==1> class="line2"<#else> class="line1"</#if> aid="${c.autoId!}">
              <td><input type="text" value="${c.itemCode}" size="7" maxlength="20" onchange="chkProd(this);"><input type="button" class="opwin" value="..." onclick="selProd(this);"/></td>
              <td><input type="text" value="<@value type="ProductcodeList" key="${c.itemCode!}"/>" size="30" readonly style="border:1px solid #eee;"></td>
              <td><input type="text" value="${c.quantity}" size="4" maxlength="4"></td>
              <td><input type="text" value="${c.price}" size="6" maxlength="8"></td>
              <td><img src="${_appPath!}/images/del.png" onclick="deli(this);" style="cursor:pointer;"/></td>
            </tr></#list></#if>
          </table>
          <div><a href="javascript:addit();" class="addline">增加商品</a></div>
        </div>

      </div>

      <div style="width:280px; float:left; margin-left:10px;">
        <div class="lsinfo" style="margin-top:10px;"><span style="float:right; margin-right:20px;"><label><input type="checkbox" onclick="selAllShop(this.checked);">全选</label></span>可售门店</div>
        <table class="list">
          <tr>
            <th width="13%">选择</th>
            <th width="30%">编号</th>
            <th width="45%" class="left">名称</th>
            <th width="10%"></th>
          </tr>
        </table>
        <div style="height:430px; overflow-y:auto;">
          <table class="list">
            <#if shopList??><#list shopList as t>
            <tr<#if t_index%2==1> class="line2"<#else> class="line1"</#if>>
              <td class="no"><input type="checkbox" name="shops" value="${t.code}" <#if t.status?? && t.status=1> checked</#if>/></td>
              <td>${t.code}</td>
              <td class="left">${t.chName}</td>
            </tr></#list></#if>
          </table>
        </div>
      </div>
      <div class="clear"></div>
    </div>
    <div class="button"><input type="submit" value="提 交"></div>
  </form>
</div>
</@page.html>